package com.wantong.jianpu.viewmodels;

import android.app.Application;

import androidx.annotation.NonNull;

import com.wantong.jianpu.base.BaseResultBean;
import com.wantong.jianpu.event.TourDetailEvent;
import com.wantong.jianpu.models.DeviceModel;
import com.wantong.jianpu.net.BaseObserver;
import com.wantong.jianpu.utils.Constants;
import com.wantong.jianpu.utils.XUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.StringUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * @author PACY
 * @date 2022/6/1
 * description:
 */
public class TourSignViewModel extends BaseViewModel {
    public SingleLiveEvent<String> time=new SingleLiveEvent<>();
    public SingleLiveEvent<String> userName=new SingleLiveEvent<>();
    public SingleLiveEvent<String> remark=new SingleLiveEvent<>();
    public String id;
    public DeviceModel model;
    public TourSignViewModel(@NonNull Application application) {
        super(application);
        model=new DeviceModel();
    }

    @Override
    protected void init() {
        super.init();
        userName.setValue(XUtils.getKey(Constants.NICKNAME));
    }

    public BindingCommand commit=new BindingCommand(() -> {
        if(StringUtils.isEmpty(time.getValue())){
            ToastUtils.show("请选择时间");
            return;
        }
        commitRequest();
    });

    public void commitRequest(){
        Map<String,Object> map=new HashMap<>();
        map.put("id",id);
        map.put("signTime",time.getValue());
        if(StringUtils.isNotEmpty(remark.getValue())){
            map.put("signRemark",remark.getValue());
        }

        showDialog();
        model.inspection_confirm(map, new BaseObserver() {
            @Override
            public void onSuccess(BaseResultBean t) {
                ToastUtils.show("提交成功");
                EventBus.getDefault().post(new TourDetailEvent());
                finish();
            }

            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
            }
        },getLifecycleProvider());

    }
}
